package servlet;

import data.RegisterUserInfo;
import data.UserInfoList;
import entity.Userinfo;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "RegisterServlet")
public class RegisterServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setHeader("Content-Type", "text/html");
        response.setCharacterEncoding("UTF-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String nickname = request.getParameter("nickname");
        RegisterUserInfo registerUserInfo = new RegisterUserInfo(username, password, nickname);
        int result = registerUserInfo.register();
        if (result > 0) {
            UserInfoList.outRegisterInfo(result, nickname);
            UserInfoList.addUser(new Userinfo(result, username, nickname));
            UserInfoList.outUserInfo();
        }
        PrintWriter printWriter = response.getWriter();
        printWriter.print(result);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        doPost(request, response);
    }
}
